let app = getApp();
var siteinfo = require('../../siteinfo.js');
Page({
  data: {
    list: [], //放置返回数据的数组
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    current: 1, //第几页,第几次加载
    number:16, //需要返回数据的个数
    floorstatus: false, 
  },

  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: options.title
    })
    this.setData({
      title: options.title
    })
    if (options.title == '证书信息') {
      this.getCertificateList()
    } else {
      this.getNewsList()
    }
  },

  onShow: function() {

  },

  //获取证书列表
  getCertificateList: function() {
    var _this = this
    _this.setData({
      current: 1,
      no_data: false
    });
    app._get(siteinfo.selIndexCertificateList, {
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      var res = result.data.records
      for (var i = 0; i < res.length; i++) {
        res[i].createrDate = res[i].createrDate.substring(0, 10)
      }
      _this.setData({
        list: res
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  //获取信息列表
  getNewsList: function() {
    var _this = this
    _this.setData({
      current: 1,
      no_data: false
    });
    app._get(siteinfo.selNewsList, {
      firstColumn: '证书考试门户',
      columnName: _this.data.title,
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      var res = result.data.records
      for (var i = 0; i < res.length; i++) {
        res[i].createrDate = res[i].createrDate.substring(0, 10)
      }
      _this.setData({
        list: res
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  onPullDownRefresh: function() {
    var _this = this
    if (_this.data.title == '证书信息') {
      this.getCertificateList()
    } else {
      this.getNewsList()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.searchScrollLower();
  },
  searchScrollLower: function() {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        current: _this.data.current + 1, //每次触发上拉事件，把current+1
      });
      wx.showLoading({
        title: '加载中',
      })
      //网络请求
      _this.reachAppendMsgList();
    }
  },
  /**
   * 上拉追加消息列表
   */
  reachAppendMsgList: function() {
    var _this = this;
    var url = null
    if (_this.data.title == '证书信息') {
      url = siteinfo.selIndexCertificateList
    }else{  //其他栏目
      url = siteinfo.selNewsList
    }
    app._get(url, {
      firstColumn: '证书考试门户',
      columnName: _this.data.title,
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      //以/n把字符串进行拆分
      var list = result.data.records;
      for (var i = 0; i < list.length; i++) {
        list[i].createrDate = list[i].createrDate.substring(0, 10)
      }
      _this.setData({
        list: _this.data.list.concat(result.data.records)
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.hideLoading();
    });
  },
  /**
   * 返回顶部
   */
  goTop: function(e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 显示/隐藏 返回顶部按钮
   */
  onPageScroll: function(e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },

  onShareAppMessage: function() {

  }
})